<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @package     Page Module
 * @author      Subha
 * @copyright   (c) 2014, 300 Development
 * @since       Version 0.1
 * @company     Matrix Infoline
 */

class Admin_banner_model extends CI_Model
{


	function __construct() {
		parent::__construct();
		log_message('debug', 'Model Initialized');

		//	$this->load->driver('session');
	}


	function getPickup($code) {
		$sql = "select * from pickup where slno != 0 and code = $code and status = 51 order by slno ASC";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
				
			return false;
		}
	}

	//--Get Banners from Banner Table (Rahul)--//
	function getBanners($featured = false, $status =1, $limit=1,$impression =NULL, $expire=NULL){


		if(!$impression){
			$sql = "Select * from gallery where status=$status and term='banner' order by id desc ";
		}
		if($impression==1){
			$sql = "Select * from gallery where status=$status and term='banner' order by impressions desc ";
		}
		if($expire==1){
			$sql = "Select *from gallery where impressions>=max_impressions and term='banner' order by id desc";

		}


		//print $clicks;
		//print $sql;

		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}

	//--Get Banners from Banner Table (Rahul)--//
	function getInactivebanners($featured = false, $status =51, $num=NULL, $offset = NULL, $limit=1,$impression =NULL, $expire=NULL){


		if(!$impression){
			$sql = "Select * from gallery where status=$status and term='banner' order by id desc limit $offset,$num ";
		}
		if($impression==1){
			$sql = "Select * from gallery where status=$status and term='banner' order by impressions desc limit $offset,$num";
		}
		if($expire==1){
			$sql = "Select *from gallery where impressions>=max_impressions and term='banner' order by id desc";

		}

		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}

	//--Delete Banner --//
	function delete_banner($id,$image_file) {
		if($id && $image_file){
			$sql="delete from gallery where id = $id";
			$this->db->query($sql);
			//--Delete Image File --//

			$f = BASEPATH.'../assets/img/advertisement/'.$image_file;
			//$f = base_url()."assets/images/data/advertisement/".$image_file;

			unlink($f);
			
			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $image_file);
			$d =  strpos($image_file, '_thumb');
			$n = substr($image_file,0,$d);
			$f = BASEPATH.'../assets/img/advertisement/'.$n.'.'.$ext;
			
			unlink($f);
		}
	}
	function search($search=null){
		$sql = "Select * from gallery where title like '%$search%' and term='banner' order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $query->result();
		}else{
			return false;
		}
	}

	function change_status($id, $status) {
		if ($status == 1) {
			$sql = "update gallery set status=51 where id=$id and term='banner'";
		}
		if ($status == 51) {
			$sql = "update gallery set status=1 where id=$id and term='banner'";
		}
		$this->db->query($sql);
	}

	function get_banner_details($id=NULL) {
		$sql = "select * from gallery where id=$id and term='banner'";
		$query = $this->db->query($sql);
		$row = $query->row();
		if($row){
			return $row;
		}else{
			
			return false;
		}
		
	}

function upload($imageData=NULL){

		if ($imageData['imagearray']['error'] == 0) {

			$ext = preg_replace("/.*\.([^.]+)$/", "\\1", strtolower($imageData['imagearray']['name']));
			$imagename=strtolower($imageData['imagearray']['name']);
			$imagename=md5(time());
			//die($imagename);
			
			$imagename=explode( '.',$imagename);
			$imagename=$imagename[0];

			if($imageData['customefilename']){
				$filename=$imageData['customefilename'].'.'.$ext;
			}
			else{
				$filename = $imagename.'.'.$ext;
			}

			if($imageData['namewithtime']=='Yes'||$imageData['namewithtime']=='yes'){
				$filename=$imagename.time().'.'.$ext;
				if($imageData['separator'])
					$filename=$imagename.$imageData['separator'].'.'.$ext;
			}
			if($imageData['prefix'])
				$filename=$imageData['prefix'].$filename;

			$config['file_name'] = strtolower($filename);
			$config['upload_path'] = $imageData['imagepath'];
			$config['allowed_types'] = $imageData['allowedtype'];
			$config['overwrite'] = $imageData['overwrite'];
			$config['remove_spaces'] = $imageData['removespace'];


			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('userfile')) {

				if($imageData['errmessage'])
					$imagedata=array('message'=>$imageData['errmessage'],'data'=>"" ,'code'=>0);
				else
					$imagedata=array('message'=>$this->upload->display_errors(),'data'=>"",'code'=>0);
				return $imagedata;
			}
			//if sucessfull



			else{
				if($imageData['createthumb']=='Yes'||$imageData['createthumb']=='yes')
				{
					
					$ext = preg_replace("/.*\.([^.]+)$/", "\\1", $filename);
						
					$img=explode( '.',$filename);
					$imgfile=$img[0];
					
					$imgfilename = $imgfile.'_thumb'.'.'.$ext;
					
					//print $filename;
					//die();
					
					
					//$thumb['imgFile'] =
					$thumb['image_library'] = 'gd2';
					$thumb['source_image'] = $imageData['imagepath'].$filename;
					$thumb['new_image'] = $imageData['thumbpath'];
					$thumb['create_thumb'] = true;
					$thumb['maintain_ratio'] = false;
					$thumb['thumb_marker']  = '_thumb' ;
					$thumb['width'] = $imageData['thumbwidth'];
					$thumb['height'] = $imageData['thumbheight'];

					$this->load->library('image_lib', $thumb);
					$this->image_lib->resize();
					if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
					$this->image_lib->clear();
				}
				if($imageData['sucessmessage'])
					$imagedata=array('message'=>$imageData['sucessmessage'],'data'=>$imgfilename,'code'=>1);
				else
					$imagedata=array('message'=>"Image Uploded Sucesfully...",'data'=>$imgfilename,'code'=>1);
				return $imagedata;
			}
		}
	}


	function count_inactive_banner($status=Null) {
		$this->db->where('status', $status);
		$query = $this->db->get('gallery');
		if ($query->num_rows() > 0) {
			return $query->num_rows();
		} else {
			return 0;
		}
	}
}